package com.example.demo.Controller;

import com.example.demo.Service.CustomerService;
import com.example.demo.repository.domain.Customer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * @author ZhangKe
 * @Description
 * @date 2019/7/3 13:19.
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    @ResponseBody
    @GetMapping("/info/{id}")
    public Customer getById(@PathVariable(value = "id") int id, HttpServletRequest request){
        Customer customer=customerService.getById(id);
        request.getSession().setAttribute("info",customer);
        return customer;
    }

    @ResponseBody
    @GetMapping("/info")
    public Customer test(HttpServletRequest request){
        return (Customer)request.getSession().getAttribute("info");
    }

    @PostMapping("/register")
    public int register(@RequestParam(value = "username")String username,
                        @RequestParam(value = "customerLevel",required = false,defaultValue = "0")Integer customerLevel,
                        @RequestParam(value = "customerPhoneNumber")String customerPhoneNumber,
                        @RequestParam(value = "password")String password,
                        @RequestParam(value = "customerAddress")String customerAddress){
        Customer customer=new Customer();
        customer.setCustomerName(username);
        customer.setCustomerLevel(customerLevel);
        customer.setCustomerPhoneNumber(customerPhoneNumber);
        customer.setCustomerPasswd(password);
        customer.setCustomerAddress(customerAddress);
        int result=customerService.register(customer);
        if (result>0){
            return customer.getCustomerNo();
        }else{
            return 0;
        }
    }

    /**
     *
     * @param username
     * @param password
     * @param request
     * @return  1:登陆成功，用户信息存入session；
     *           0：密码错误
     *           -1:用户不存在
     */
    @PostMapping("/login")
    public Customer login(@RequestParam(value = "username")String username,
                     @RequestParam(value = "password")String password,
                     HttpServletRequest request){
        int result=customerService.login(username,password);
        if (result==1){
            Customer customer=customerService.getByName(username);
            request.getSession().setAttribute("info",customer);
            return customer;
        }else
            return null;
    }

    /**
     *
     * @param username
     * @param phoneNumber
     * @param customerAddress
     * @return  更改成功，用户信息存入session；
     */
    @PostMapping("/update")
    public Customer update(@RequestParam(value = "customerNo")Integer customerNo,
                           @RequestParam(value = "username",required = false)String username,
                           @RequestParam(value = "phoneNumber",required = false)String phoneNumber,
                           @RequestParam(value = "customerAddress",required = false)String customerAddress){
        Customer result=customerService.getById(customerNo);
        if (username!=null){
            result.setCustomerName(username);
        }
        if (phoneNumber!=null){
            result.setCustomerPhoneNumber(phoneNumber);
        }
        if (customerAddress!=null){
            result.setCustomerAddress(customerAddress);
        }
        customerService.update(result);
        return result;
    }

}
